home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / jDXEngine 23886812001.psc / jDXEngine.cls < prev   
Encoding:
Visual Basic class definition  |  2001-08-01  |  8.0 KB  |  222 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "jDXEngine"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'Ahhh... the powerful and might jDXEngine. ;)
  17. 'This is meant to be the mother of all the other 'helper' objects that are contained in this project.
  18. 'Note that the other objects CAN be used independently, but will probably need a little bit of re-coding
  19. 'to make them work alone.  All helper objects should be able to be accessed through jDXEngine, so declaring
  20. 'them seperately is quite unneccessary.  If you have any questions/comments on this object, please direct
  21. 'them to jhicks@hsadallas.com.  In case anone is wondering where the 'j' in jDXEngine comes from, my name
  22. 'is 'j'oe, and I've gotten into the habit of prefixing my own personal object classes with j. :)
  23.  
  24. 'Declare DirectX objects needed
  25. Dim DX As DirectX8                  'The mother of all DirectX objects
  26. Dim D3D As Direct3D8                'Handles 3D stuffs
  27. Dim D3DX As D3DX8                   'Helper library for meshes and such (will be implemented next release)
  28. Dim D3DDevice As Direct3DDevice8    'Device to draw upon... (almost anything with a .hWnd property)
  29.  
  30. 'Local class variables for use in rendering
  31. Dim bRunning As Boolean             'Quit rendering when False
  32. Dim BackColor As Long               'Background color of the drawing surface
  33.  
  34. 'Camera object
  35. Dim mCamera As New clsCamera
  36.  
  37. 'Keyboard object
  38. Dim mKeyboard As New clsKeyboard
  39.  
  40. 'Lights object(s)
  41. Dim mLights As New clsLights
  42.  
  43. 'Value of pi (4*atn(1)) for any geometry calculations that may need to be performed
  44. Const mPI = 3.14159265358979
  45.  
  46. 'Vertex type definitions to use
  47. Const FVF_LVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR Or D3DFVF_TEX1)    'Use without lighting
  48. Const FVF_UNLITVERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1)                    'Use with lighting
  49.  
  50. 'Matrices
  51. Dim matWorld As D3DMATRIX   'World matrix... when you rotate this, you rotate the entire world
  52. Dim matProj As D3DMATRIX    'Projection matrix... used to define the 'lens' for the camera
  53.                             'matProj SHOULD be with the camera object, but until I understand it's use
  54.                             'better, it will remain here.
  55. Dim matTemp As D3DMATRIX    'Temporary matrix used in calculations
  56.  
  57. 'Storage for all textures that may be loaded
  58. Dim colTextures As New collection
  59.  
  60. 'Unlit geometry object collection.  Each is it's own 'entity'
  61. Private UnlitGeometry As New collection
  62.  
  63.  
  64. 'A simple sub to add a clsUnlitObject object to the collection
  65. Public Sub AddGeometry(UnlitGeo As clsUnlitObject)
  66.     UnlitGeometry.Add UnlitGeo
  67. End Sub
  68.  
  69. 'This function will load a texture from a file, and store it into a collection using a programmer-provided
  70. 'key to reference it by.  If the function is successful, it will return the reference name of the texture.
  71. 'Otherwise, it will return an empty string to indicate failure
  72. 'strFileName:       Full path and filename of the image to be loaded
  73. 'strTextureName:    Name used to reference this texture
  74. Public Function AddTexture(strFileName As String, strTextureName As String) As String
  75.     On Error GoTo error_h
  76.     
  77.     'Temporary storage for the texture
  78.     Dim Texture As Direct3DTexture8
  79.     
  80.     'First make sure the file exists...
  81.     If Dir(strFileName) = "" Then Exit Function
  82.     
  83.     'Validate the texture name
  84.     If strTextureName = "" Or IsInCollection(colTextures, strTextureName) Then Exit Function
  85.         
  86.     'Create the texture
  87.     Set Texture = D3DX.CreateTextureFromFile(D3DDevice, strFileName)
  88.     
  89.     'Make sure that the texture was created properly
  90.     If Texture Is Nothing Then Exit Function
  91.     
  92.     'Add this texture to the collection
  93.     colTextures.Add Texture, strTextureName
  94.     
  95.     'If all went well, return the name of the texture
  96.     AddTexture = strTextureName
  97.     
  98.     Exit Function
  99. error_h:
  100.     Select Case ErrMsg(Err, "jDXEngine.AddTexture(" & strFileName & "," & strTextureName & ")")
  101.         Case vbRetry
  102.             Resume
  103.         Case vbIgnore
  104.             Resume Next
  105.         Case Else
  106.             Exit Function
  107.     End Select
  108. End Function
  109.  
  110. 'This allows programmer-access to the camera object
  111. Property Get Camera() As clsCamera
  112.     Set Camera = mCamera
  113. End Property
  114.  
  115. 'Returns a texture when given the reference name of an existing texture
  116. 'This function will return NOTHING if the reference name provided does not exist
  117. Public Function GetTexture(strTexture As String) As Direct3DTexture8
  118.     On Error GoTo error_h
  119.     
  120.     If IsInCollection(colTextures, strTexture) Then
  121.         Set GetTexture = colTextures(strTexture)
  122.     Else
  123.         Set GetTexture = Nothing
  124.     End If
  125.     
  126.     Exit Function
  127. error_h:
  128.     Select Case ErrMsg(Err, "jDXEngine.GetTexture(" & strTexture & ")")
  129.         Case vbRetry
  130.             Resume
  131.         Case vbIgnore
  132.             Resume Next
  133.         Case Else
  134.             Exit Function
  135.     End Select
  136. End Function
  137.  
  138. 'This function will determine if an item with a specific key exists within a collection
  139. Private Function IsInCollection(collection As collection, strKey As String) As Boolean
  140.     On Error GoTo error_h
  141.     
  142.     On Error Resume Next
  143.     Dim X As Direct3DTexture8
  144.     Set X = collection(strKey)
  145.     If Err Then
  146.         Err.Clear
  147.         IsInCollection = False
  148.     Else
  149.         IsInCollection = True
  150.     End If
  151.     
  152.     Exit Function
  153. error_h:
  154.     Select Case ErrMsg(Err, "jDXEngine.IsInCollection")
  155.         Case vbRetry
  156.             Resume
  157.         Case vbIgnore
  158.             Resume Next
  159.         Case Else
  160.             Exit Function
  161.     End Select
  162. End Function
  163.  
  164. 'r=red value(0-255),g=green,b=blue
  165. Public Function jRGB(r As Integer, g As Integer, b As Integer) As Long
  166.     On Error GoTo error_h
  167.     
  168.     'I've noticed when working with DX8 that the long color code values it uses
  169.     'are exactly the same as VB's RGB() function except that the R and B values
  170.     'are transposed, so I wrote this little helper function to help out with that.
  171.     jRGB = RGB(b, g, r)
  172.     
  173.     Exit Function
  174. error_h:
  175.     Select Case ErrMsg(Err, "jDXEngine.DXRGB(" & r & "," & g & "," & b & ")")
  176.         Case vbRetry
  177.             Resume
  178.         Case vbIgnore
  179.             Resume Next
  180.         Case Else
  181.             Exit Function
  182.     End Select
  183. End Function
  184.  
  185. Public Sub EndRender()
  186.     On Error GoTo error_h
  187.     
  188.     'All we need to do to stop rendering is set this little variable here to false. :)
  189.     bRunning = False
  190.     
  191.     Exit Sub
  192. error_h:
  193.     Select Case ErrMsg(Err, "jDXEngine.EndRender")
  194.         Case vbRetry
  195.             Resume
  196.         Case vbIgnore
  197.             Resume Next
  198.         Case Else
  199.             Exit Sub
  200.     End Select
  201. End Sub
  202.  
  203. 'hWnd = the handle to the window that you want to draw in (i THINK it can be anything with a hWnd property, but don't quote me on that)
  204. 'HardwareDevice = true if you want to use a 3d accelorator card... false if not
  205. 'vtrCamPos = a vector desribing the initial position of the camera
  206. 'lngBackColor = the background color when rendering
  207. 'blnUseLighting = True to enable lighting
  208. 'blnCullCCW = True to enable back-face removal
  209. Public Function InitWindowed(hWnd As Long, HardwareDevice As Boolean, vtrCamPos As D3DVECTOR, lngBackColor As Long, blnUseLighting As Boolean, blnCullCCW As Boolean) As Boolean
  210.     On Error GoTo error_h
  211.     
  212.     'Boolean values default to false, but just to be safe...
  213.     InitWindowed = False
  214.     
  215.     'Declare variables to store display information
  216.     Dim d3dpp As D3DPRESENT_PARAMETERS
  217.     Dim DispMode As D3DDISPLAYMODE
  218.     
  219.     'Create the main 3 objects (D3DX8 is NOT one of the main 3... D3DDevice is the 3rd, but we must configure it first)
  220.     Set DX = New DirectX8
  221.     Set D5 .wAs Btt teust to be safe...
  222.     tosafe textdTigure it B